<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="版本标识" name="version">
						<a-input v-model:value="searchFormState.version" placeholder="请输入版本标识" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="名称" name="name">
						<a-input v-model:value="searchFormState.name" placeholder="请输入名称" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="说明" name="description">
						<a-input v-model:value="searchFormState.description" placeholder="请输入说明" />
					</a-form-item>
				</a-col>
				<a-col :span="6" v-show="advanced">
					<a-form-item label="生成类型" name="builderType">
						<a-select v-model:value="searchFormState.builderType" placeholder="请选择生成类型" :options="builderTypeOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
					</a>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('softwareVersionAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-button
						v-if="hasPerm('softwareVersionBatchDelete')"
						buttonName="批量删除"
						icon="DeleteOutlined"
						buttonDanger
						:selectedRowKeys="selectedRowKeys"
						@batchCallBack="deleteBatchSoftwareVersion"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'builderType'">
					{{ $TOOL.dictTypeData('BUILD_TYPE', record.builderType) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('softwareVersionEdit')">编辑</a>
						<a @click="viewRef.onOpen(record)" >查看</a>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteSoftwareVersion(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('softwareVersionDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
	<View ref="viewRef" @successful="tableRef.refresh()" />
</template>

<script setup name="softwareversion">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import View from './view.vue'
	import softwareVersionApi from '@/api/biz/softwareVersionApi'
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const viewRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '名称',
			dataIndex: 'name'
		},
		{
			title: '说明',
			dataIndex: 'description'
		},
		{
			title: '生成类型',
			dataIndex: 'builderType'
		},
		{
			title: '程序路径',
			dataIndex: 'path'
		},
		{
			title: '生成脚本执行命令',
			dataIndex: 'scriptName'
		},
		{
			title: '用户信息文件名',
			dataIndex: 'userInfoFileName'
		},
		{
			title: '配置文件文件名',
			dataIndex: 'configFileName'
		},
		{
			title: '授权文件产物名',
			dataIndex: 'licenseFileName'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['softwareVersionEdit', 'softwareVersionDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 200
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		return softwareVersionApi.softwareVersionPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteSoftwareVersion = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		softwareVersionApi.softwareVersionDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchSoftwareVersion = (params) => {
		softwareVersionApi.softwareVersionDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
	const builderTypeOptions = tool.dictList('BUILD_TYPE')
</script>
